#include "libLUI_in.h"

#define _EVENT_TRACE	0

struct LUIEvent *LUIAllocMessage( struct LUIWindowsManager *windows_manager , struct LUIWindow *window , struct LUIControl *control )
{
	struct LUIEvent		*event = NULL ;
	
	event = (struct LUIEvent *)malloc( sizeof(struct LUIEvent) ) ;
	if( event == NULL )
		return NULL;
	memset( event , 0x00 , sizeof(struct LUIEvent) );
	
	event->window = window ;
	event->control = control ;
	gettimeofday( & (event->timestamp) , NULL );
	event->cursor_pos.x = windows_manager->cursor_x ;
	event->cursor_pos.y = windows_manager->cursor_y ;
	
	return event;
}

int LUISendMessage( struct LUIWindowsManager *windows_manager , struct LUIWindow *window , struct LUIControl *control , int device , int _event , int code )
{
	struct LUIEvent		*event = NULL ;
	
	event = LUIAllocMessage( windows_manager , window , control ) ;
	
	if( event )
	{
		event->device = device ;
		event->event = _event ;
		event->code = code ;
		return LUISendMessageEx( windows_manager , event );
	}
	else
	{
		return -1;
	}
}

int LUISendMessageEx( struct LUIWindowsManager *windows_manager , struct LUIEvent *event )
{
	list_add_tail( & (event->this_listnode) , & (windows_manager->events_list.this_listnode) );
	
	return 0;
}

struct LUIEvent *LUIFetchMessage( struct LUIWindowsManager *windows_manager )
{
	struct LUIEvent		*event = NULL ;
	
	event = list_first_entry_or_null( & (windows_manager->events_list.this_listnode) , struct LUIEvent , this_listnode ) ;
	if( event == NULL )
		return NULL;
	
	list_del( & (event->this_listnode) );
	
	return event;
	
}

int LUIProcessMessage( struct LUIEvent *event )
{
	int		nret = 0 ;
	
#if _EVENT_TRACE
	if( event->device == LUIDEVICE_MOUSE && event->event == LUIEVENT_WINDOW_MOVE )
		printf( "timestamp[%ld.%06ld] - window[%p] control[%p] device[%d] event[%d] code[%d] - relative_offset.left[%d] .top[%d]\n" , event->timestamp.tv_sec , event->timestamp.tv_usec , event->window , event->control , event->device , event->event , event->code , event->data.relative_offset.left , event->data.relative_offset.top );
	else if( event->device == LUIDEVICE_MOUSE && event->event == LUIEVENT_WINDOW_RESIZE )
		printf( "timestamp[%ld.%06ld] - window[%p] control[%p] device[%d] event[%d] code[%d] - window_rect.left[%d] .top[%d] .width[%d] .height[%d]\n" , event->timestamp.tv_sec , event->timestamp.tv_usec , event->window , event->control , event->device , event->event , event->code , event->data.window_rect.left , event->data.window_rect.top , event->data.window_rect.width , event->data.window_rect.height );
	else
		printf( "timestamp[%ld.%06ld] - window[%p] control[%p] device[%d] event[%d] code[%d]\n" , event->timestamp.tv_sec , event->timestamp.tv_usec , event->window , event->control , event->device , event->event , event->code );
#endif
	
	if( event->control )
	{
		if( event->control->pfuncLUIControlEventProc )
		{
			nret = event->control->pfuncLUIControlEventProc( event->control , event ) ;
		}
	}
	else if( event->window )
	{
		if( event->event == LUIEVENT_WINDOW_DESTROY )
		{
			nret = LUIDestroyWindow( event->window ) ;
			if( nret )
				return nret;
		}
		else if( event->window->pfuncLUIWindowEventProc )
		{
			nret = event->window->pfuncLUIWindowEventProc( event->window , event ) ;
		}
	}
	
	free( event );
	
	return nret;
}

